﻿using PlutoStudio.Net.Messages;
using PlutoStudio.Net.Processors;
using PlutoStudio.Net.Processors.Security;
using PlutoStudio.Net.Tcp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlutoStudio.Net.Server
{
    public class NetContext:PlutoStudio.Net.Processors.NetContext
    {
        public object UserData { get; set; }


        public virtual void Login(string userName)
        {
            this.User = new PlutoStudio.Net.Processors.Security.GenericPrincipal(new PlutoStudio.Net.Processors.Security.GenericIdentity(userName, "Form"), null);
        }

        public virtual void Logout()
        {
            this.User = new GenericPrincipal(new GenericIdentity(), null);
        }

        protected override string ProcessorConfigName
        {
            get { return "Processor.config"; }
        }

        public NetContext(INetClient client):base(client)
        {
            client.Closed += client_Closed;
        }

        void client_Closed(object sender, EventArgs e)
        {
            Logout();
        }


        protected override void OnLoadProcessFaild(Message message, TypeLoadException ex)
        {
            var result = new Message(message.Flag, message.Code, (ushort)StatusCode.NotImplemented);
            message.SetData(ex.Message);
            Client.Send(result);
        }
    }
}
